import { Component, OnInit } from "@angular/core";
import { Iwe7FormBuilderService } from "../iwe7-form-builder.service";
import { Router, ActivatedRoute } from "@angular/router";
@Component({
  selector: "create-modal",
  templateUrl: "./create-modal.component.html",
  styleUrls: ["./create-modal.component.scss"],
  providers: [
    Iwe7FormBuilderService
  ]
})
export class CreateModalComponent implements OnInit {
  types: any[] = [
    {
      title: "文本框",
      value: "text"
    },
    {
      title: "电话号码",
      value: "mobile"
    },
    {
      title: "邮箱",
      value: "email"
    },
    {
      title: "文本域",
      value: "textarea"
    },
    {
      title: "单选",
      value: "radio"
    },
    {
      title: "多选",
      value: "checkbox"
    },
    {
      title: "单图",
      value: "image"
    },
    {
      title: "多图",
      value: "thumb"
    },
    {
      title: "地理位置",
      value: "location"
    }
  ];

  id: any  = 0;
  constructor(
    public fb: Iwe7FormBuilderService,
    public router: Router,
    public route: ActivatedRoute
  ) {
    this.route.queryParams.subscribe(res => {
      this.id = res.id || 0;
      this.getDetail(this.id);
    });
  }

  ngOnInit() {}

  getDetail(id: any) {
    this.fb.getDetail(id).subscribe(res => {
      this.fb.editForm(res);
    });
  }

  save() {
    this.fb.save(this.id).subscribe(res => {
      this.router.navigate(["/formBuilder/formList"]);
    });
  }
}
